import React, { RefObject, useEffect, useState } from "react";
// 自定义hooks
function useClickOutside(ref: RefObject<HTMLElement>, handler: Function) {
    useEffect(() => {
        // 监听点击事件
        const listener = (e: MouseEvent) => {
            if (!ref.current || ref.current.contains(e.target as HTMLElement)) {
                return;
            }
            handler(e);
        }
        document.addEventListener('click', listener);
        // 删除监听器
        return () => {
            document.removeEventListener('click', listener);
        }
    }, [ref, handler]);
    return useClickOutside;
}
export default useClickOutside;